package contextManager

import scala.actors.Actor
import scala.actors.Actor._
import scala.collection.mutable.ListBuffer
import messages._

object ContextManager extends Actor{
	val implementations = new ListBuffer[(Adaptable,Context => Boolean,Implementation)]

	def act() {
		while (true) {
			receive {
				case context : Context =>{
					println("CONTEXT-MANAGER: requesting context-switch")
					for(registeredFunction <- implementations){
						if(registeredFunction._2(context)){
							registeredFunction._1 ! registeredFunction._3
						}
					}
				}
				case Stop => return
			}
		}
	}
	
	def registerListener
			(
			name:String,
			activationFunction:Context => Boolean,
			function:Nothing=>Any)
			(implicit adaptable:Adaptable)
			= {
	  implementations+=((adaptable,activationFunction,new Implementation(name,function)))
  }
}